<template>
  <div class="container">
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span>修改密码</span>
      </div>
      <el-form ref="passwordForm" :model="passwordForm" :rules="rules" label-width="0">
        <el-form-item prop="account">
          <el-row class="info">
            <img src="../../assets/images/login/3.png" alt="" width="24">
            账号
          </el-row>
          <el-input v-model="passwordForm.account" placeholder="请输入账号" readonly>

          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-row class="info">
            <img src="../../assets/images/login/4.png" alt="" width="24">
            原密码
          </el-row>
          <el-input v-model.trim="passwordForm.password" placeholder="请输入原密码">
          </el-input>
        </el-form-item>
        <el-form-item prop="newPassword">
          <el-row class="info">
            <img src="../../assets/images/login/4.png" alt="" width="24">
            输入新密码
          </el-row>
          <el-input v-model.trim="passwordForm.newPassword" placeholder="设置6-20位登陆密码" @input="check">
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-row class="info">
            <img src="../../assets/images/login/4.png" alt="" width="24">
            确认新密码
          </el-row>
          <el-input v-model.trim="passwordForm.confirmPassword" placeholder="请确认密码" @input="check">
          </el-input>
        </el-form-item>
      </el-form>
    </el-card>
    <el-button class="themeButton"
               @click="modifyPwd"
               :disabled="disableFlag">
      确认修改
    </el-button>

    <el-dialog
      :visible.sync="dialogVisible"
      :show-close="false"
      custom-class="successMsg"
      @close="$router.push('/profile')"
    >
      <div slot="title">
        <img src="../../assets/images/withdraw/success.png" alt="" width="43">
      </div>
      <span style="font-size: 18px;
                   font-weight: bold;
                   text-align: center;
                   color: #333333;">
        修改成功!
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {modifyPwd} from '@/api/modifyPwd'

export default {
  name: "changePwd",
  data() {
    return {
      passwordForm: {
        account: "",
        password: "",
        newPassword: "",
        confirmPassword: ""
      },
      userToken: '',
      rules: {
        password: [
          {required: true, message: "请输入原密码", trigger: "blur"}
        ],
        newPassword: [
          {required: true, message: "请输入新密码", trigger: "blur"}
        ],
        confirmPassword: [
          {required: true, message: "请再次输入密码", trigger: "blur"}
        ]
      },
      disableFlag: true,
      dialogVisible: false
    }
  },
  created() {
    const that = this;
    that.userToken = localStorage.getItem("token")
    that.passwordForm.account = localStorage.getItem("account")
  },
  methods: {
    modifyPwd() {
      const that = this
      let password = localStorage.getItem("password")
      if (that.passwordForm.password !== password) {
        this.$notify.error({
          title: '错误',
          message: "原密码错误",
          showClose: false,
          duration: 1500
        });
      } else if (that.passwordForm.newPassword !== that.passwordForm.confirmPassword) {
        this.$notify.error({
          title: '错误',
          message: "两次输入密码不一致",
          showClose: false,
          duration: 1500
        });
      } else {
        modifyPwd(
          that.userToken,
          that.passwordForm
        ).then(res => {
          this.$notify({
            title: '修改成功！',
            message: "3秒后自动跳转...",
            type: 'success',
            showClose: false,
            duration: 3000
          })
          setTimeout(() => {
            that.$router.go(-1);
          }, 2500)
        }).catch((err) => {
          this.$notify.error({
            title: '错误',
            message: err,
            showClose: false,
            duration: 2000
          });
        })
      }
    },
    check() {
      let arr = []
      for (let key in this.passwordForm) {
        arr.push(this.passwordForm[key])
      }
      this.disableFlag = !arr.every((r) => r !== "")
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  width: 100%;

  .clearfix {
    text-align: left;
    font-size: 16px;
    color: #1e2431;
    font-weight: bold;
  }
}

.el-card {
  box-sizing: border-box;
  padding: 0 16px 24px;
  border: 0;
  border-radius: 4px;

  & > > > .el-card__header {
    padding: 14px 0;
  }

  & > > > .el-card__body {
    padding: 0;
  }

  & > > > .el-input__inner {
    border-top: 0;
    border-left: 0;
    border-right: 0;
    height: 50px;
    line-height: 50px;
    padding: 0;
    border-radius: 0;
    border-bottom: 1px solid #EBEEF5;
  }

  & > > > .el-input {
    height: 40px;
  }

  & > > > .el-form-item {
    margin: 0;

    .el-form-item__error {
      top: calc(50% + 15px);
      left: calc(100% - 80px);
      width: fit-content;
    }
  }
}

.info {
  display: flex;
  align-items: center;
  font-size: 14px;
  text-align: left;
  color: #1e2431;
  padding-top: 6px;

  img {
    margin-right: 6px;
  }
}

.themeButton {
  width: 100%;
  height: 42px;
  margin-top: 16px;
  color: #fff;
  opacity: 1;
  background: linear-gradient(90deg, rgba(253, 110, 5, 0.66) 10%, #fd6e05 81%);
  border-radius: 21px;
  border: none;
  transition: all .5s ease;

  &:hover,
  &:active {
    filter: brightness(1.1);
    box-shadow: 0 7px 25px 0 rgba(253, 110, 5, 0.41);
  }
}

.el-button.is-disabled {
  opacity: 0.3;
}

.el-button.is-disabled:hover, .el-button.is-disabled:focus {
  color: #fff;
  background: linear-gradient(90deg, rgba(253, 110, 5, 0.66) 10%, #fd6e05 81%);;
}

> > > .successMsg {
  margin-top: 25vh !important;
  width: calc(100% - 30px) !important;
  border-radius: 12px;

  .el-dialog__header {
    padding: 30px 0 20px;
  }

  .el-dialog__body {
    width: 100%;
    padding: 0 0 30px;
    text-align: center;
  }

  .el-dialog__footer {
    padding: 0 34px 34px;
  }

  .themeButton {
    height: 36px;
  }
}
</style>
